package com.hellomaker.redismq.starter.config.parser;

import com.hellomaker.redismq.consume.listener.ListenerContainer;
import com.hellomaker.redismq.core.mq.SimpleRedisMQClient;
import com.hellomaker.redismq.starter.config.ListenerAnnotationParser;
import com.hellomaker.redismq.starter.config.MessageListener;
import com.hellomaker.redismq.starter.config.listener.ListenerMethod;

import java.lang.reflect.Method;

public class MessageListenerMethodParser implements ListenerAnnotationParser {

    SimpleRedisMQClient client;
    ListenerContainer<String, String> listenerContainer;
    public MessageListenerMethodParser(SimpleRedisMQClient client, ListenerContainer<String, String> listenerContainer) {
        this.client = client;
        this.listenerContainer = listenerContainer;
    }

    @Override
    public void parseBean(Class<?> aClass, Object bean, String beanName) {
        Method[] declaredMethods = aClass.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            if (declaredMethod.isAnnotationPresent(MessageListener.class)) {
                MessageListener messageListener = declaredMethod.getAnnotation(MessageListener.class);
                ListenerMethod listenerMethod = new ListenerMethod(client, listenerContainer, messageListener, declaredMethod, bean);
                listenerMethod.doListen();
            }
        }
    }
}
